﻿package MyComponent// ActionScript file
{
	import MyComponent.Events.MyScrollEvent;
	
	import fl.core.InvalidationType;
	import fl.core.UIComponent;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;

	public class MyScrollPanel extends UIComponent
	{
		private var _scrollBar:MyScrollBar;
		private var _background:DisplayObject;
		private var _container:Sprite;
		private var _scrollPolicy:String="auto";
		private static var defaultStyles:Object = {scrollBarType:"MyScrollBar_default",backgroundSkin:"MyScrollPanel_default_background"};
		
		public function MyScrollPanel():void
		{
			super();
			this.setStyles();
			this.addListener();
		}
		
		override protected function configUI():void
		{
			super.configUI();
			
			this.container=new Sprite;
			this.addChild(this.container);
			
			this.scrollBar=new MyScrollBar();
			this.addChild(this.scrollBar);
		}
		
		private function addListener():void
		{
			this.scrollBar.addEventListener(MyScrollEvent.Scroll,scrollHandler,false,0,true);
		}
		
		private function scrollHandler(e:MyScrollEvent):void
		{
			if(e.maxScrollPosition && this.container.height>this.height)
			{
			    this.container.y=-e.scrollPosition/e.maxScrollPosition*(this.container.height-this.height);
			}
		}
		
		public function containerChanged():void
		{
			this.invalidate(InvalidationType.STATE);
		}
		
		private function updateContainer():void
		{
			this.scrollBar.pageTotalSize=this.container.height;
			if(this.scrollPolicy=="auto")
			{
				if(this.container.height>this.container.mask.height)
				{
					this.scrollBar.visible=true;
				}
				else
				{
					this.scrollBar.visible=false;
				}
			}
		}
		
		public static function getStyleDefinition():Object
		{
			return defaultStyles;
		}
		
		private function drawMask():void
		{
			var mask:Sprite=Sprite(this.container.mask);
			if(mask)
			{
				mask.graphics.clear();
			}
			else
			{
				mask=new Sprite;
				this.container.mask=mask;
				this.addChild(mask);
			}
			mask.graphics.beginFill(0x000000,0);
			mask.graphics.drawRect(0,0,super.width,super.height);
			mask.graphics.endFill();
		}
		
		private function setStyles():void
		{
			this.scrollBar.skinsType=String(this.getStyleValue("scrollBarType"));
			if(this.background)this.removeChild(this.background);
			this.background=this.getDisplayObjectInstance(this.getStyleValue("backgroundSkin"));
			if(this.background)this.addChildAt(this.background,0);
			this.background.width=super.width;
			this.background.height=super.height;
		}
		
		override protected function draw():void
		{
			if(this.isInvalid(InvalidationType.SIZE))
			{
				this.scrollBar.move(super.width,0);
				this.scrollBar.height=super.height;
				this.scrollBar.pageSize=super.height;
				this.drawMask();
			}
			if(this.isInvalid(InvalidationType.STYLES))
			{
				this.setStyles();
			}
			if(this.isInvalid(InvalidationType.STATE))
			{
				this.updateContainer();
			}
			this.scrollBar.drawNow();
			this.validate();
		}

		/*getter & setter*/
		public function get container():Sprite
		{
			return _container;
		}

		public function set container(value:Sprite):void
		{
			_container = value;
		}

		public function get scrollBar():MyScrollBar
		{
			return _scrollBar;
		}

		public function set scrollBar(value:MyScrollBar):void
		{
			_scrollBar = value;
		}

		[Inspectable(enumeration="auto,show,hide",defaultValue="auto",type="String")]
		public function get scrollPolicy():String
		{
			return _scrollPolicy;
		}
		
		public function set scrollPolicy(value:String):void
		{
			_scrollPolicy = value;
			switch(value)
			{
				case "show":
					this.scrollBar.visible=true;
					break;
				case "hide":
					this.scrollBar.visible=false;
					break;
				case "auto":
					this.containerChanged();
					break;
			}
		}
		
		[Inspectable(defaultValue="MyScrollPanel_default_background",type="String")]
		public function set backgroudSkin(value:String):void
		{
			if(this.getStyle("backgroundSkin")==value)return;
			this.setStyle("backgroundSkin",value);
			this.invalidate(InvalidationType.STYLES);
		}
		
		[Inspectable(defaultValue="MyScrollBar_default",type="String")]
		public function set scrollBarSkins(value:String):void
		{
			if(this.getStyle("scrollBarType")==value)return;
			this.setStyle("scrollBarType",value);
			this.invalidate(InvalidationType.STYLES);
		}
		
		[Inspectable(defaultValue=false,type="Boolean")]
		public function get thumbHeightLocked():Boolean
		{
			return this.scrollBar.thumbHeightLocked;
		}
		
		public function set thumbHeightLocked(value:Boolean):void
		{
			this.scrollBar.thumbHeightLocked=value;
		}

		public function get background():DisplayObject
		{
			return _background;
		}

		public function set background(value:DisplayObject):void
		{
			_background = value;
		}


	}
}